import Vue from 'vue'
/**
 * 省市区
 */
export default class Area {
  /**
   * 构造器
   * @param express
   */
  constructor(pcaa) {
    if (!pcaa) {
      pcaa = Vue.prototype.$Jpcaa
    }
    let arr = []
    const province = pcaa['86']
    Object.keys(province).map((key) => {
      arr.push({ id: key, text: province[key], pid: '86', index: 1 })
      const city = pcaa[key]
      Object.keys(city).map((key2) => {
        arr.push({ id: key2, text: city[key2], pid: key, index: 2 })
        const qu = pcaa[key2]
        if (qu) {
          Object.keys(qu).map((key3) => {
            arr.push({ id: key3, text: qu[key3], pid: key2, index: 3 })
          })
        }
      })
    })
    this.all = arr
    this.pcaaCustom = pcaa
  }

  get pca() {
    return this.all
  }

  getCode(text) {
    if (!text || text.length == 0) {
      return ''
    }
    for (let item of this.all) {
      if (item.text === text) {
        return item.id
      }
    }
  }

  getText(code) {
    if (!code || code.length == 0) {
      return ''
    }
    let arr = []
    this.getAreaBycode(code, arr, 3)
    return arr.join('/')
  }

  getRealCode(code) {
    let arr = []
    this.getPcode(code, arr, 3)
    return arr
  }

  getPcode(id, arr, index) {
    for (let item of this.all) {
      if (item.id === id && item.index == index) {
        arr.unshift(id)
        if (item.pid != '86') {
          this.getPcode(item.pid, arr, --index)
        }
      }
    }
  }

  getAreaBycode(code, arr, index) {
    for (let item of this.all) {
      if (item.id === code && item.index == index) {
        arr.unshift(item.text)
        if (item.pid != '86') {
          this.getAreaBycode(item.pid, arr, --index)
        }
      }
    }
  }
  getAreaByAddress(text) {
    if (!text) {
      return {
        type: 'error',
        message: '未识别到省市区信息！',
      }
    }
    //1. 判断省级是否包含
    let province = this.pcaaCustom[86]
    let pValue = Object.values(province)
    let pResult = '' //省份
    let cResult = '' //市级
    let aResult = '' //区级
    let pCode = ''
    let cCode = ''
    let aCode = ''

    //1. 获取省份全称
    for (let index = 0; index < pValue.length; index++) {
      const pName = pValue[index]
      if (text.indexOf(pName) > -1) {
        pResult = pName
        break
      } else {
        pResult = this.getSpecificProvince(pName, text)
        if (pResult) {
          break
        }
      }
    }
    // 2，未获取到省份时循环市级
    if (!pResult) {
      let pKeys = Object.keys(province)
      for (let index = 0; index < pKeys.length; index++) {
        if (cResult) {
          break
        }
        //市级列表
        let cMap = this.pcaaCustom[pKeys[index]]
        let cKeys = Object.keys(cMap)
        for (let i = 0; i < cKeys.length; i++) {
          let key = cKeys[i]
          let cName = cMap[key]
          if (cName.endsWith('市')) {
            cName = cName.substr(0, cName.length - 1)
          }
          if (text.indexOf(cName) > -1) {
            cResult = cMap[key]
            cCode = key
            pResult = province[pKeys[index]]
            pCode = pKeys[index]
            break
          }
        }
      }
    } else {
      pCode = this.getCode(pResult)
      if (pResult.endsWith('市')) {
        let sxq = this.pcaaCustom[pCode] //市辖区
        cCode = Object.keys(sxq)[0] //获取市辖区code
        cResult = '市辖区'
      } else {
        //市级列表
        let cMap = this.pcaaCustom[pCode]
        let cKeys = Object.keys(cMap)
        for (let index = 0; index < cKeys.length; index++) {
          let key = cKeys[index]
          let cName = cMap[key]
          if (cName.endsWith('市')) {
            cName = cName.substr(0, cName.length - 1)
          }
          if (text.indexOf(cName) > -1) {
            cResult = cMap[key]
            cCode = key
            break
          }
        }
      }
    }

    if (pResult && cResult && cCode) {
      if (pResult.indexOf('特别行政区') == -1) {
        let aMap = this.pcaaCustom[cCode]
        let aKeys = Object.keys(aMap)
        for (let index = 0; index < aKeys.length; index++) {
          const key = aKeys[index]
          let aName = aMap[key]
          if (text.indexOf(aName) > -1) {
            aResult = aName
            aCode = key
            break
          }
        }
        // 省市区
        return {
          type: 'success',
          code: [pCode, cCode, aCode],
          name: [pResult, cResult, aResult],
          message: `识别结果：${pResult}/${cResult}/${aResult}`,
        }
      } else {
        // 特别行政区
        return {
          type: 'success',
          code: [pCode, cCode],
          name: [pResult, cResult],
          message: `识别结果：${pResult}/${cResult}`,
        }
      }
    } else if (pResult) {
      // 只识别到省份
      return {
        type: 'warning',
        code: [pCode],
        message: `识别结果：${pResult}`,
      }
    } else {
      return {
        type: 'error',
        message: '未识别到省市区信息！',
      }
    }
  }
  getSpecificProvince(pName, text) {
    let sps = ['省', '市', '自治区', '壮族自治区', '回族自治区', '维吾尔自治区', '特别行政区']
    for (let index = 0; index < sps.length; index++) {
      const sp = sps[index]
      if (pName.indexOf(sp) > -1) {
        let p = pName.substr(0, pName.length - sp.length)
        if (text.indexOf(p) > -1) {
          return pName
        }
      }
    }
    return ''
  }
}

